<template>
	<view>
		<!-- 弹框 在这里 -->
		<view class="popp-bg" v-if="showTips">
			<view class="popp">
				<view class="title">发现新版本</view>
				<view class="new-version-title" style="margin-bottom: 164upx;">最新版本{{newVersion}}</view>
				<view class="popp-item" v-for="(item,i) in note" :key="i">{{(i+1)}}、{{item}}</view>
				<view class="btn-cont">
					<view class="btn-cont-item btn-cont-itemA" @click="nowUpdate(url,type)">立即升级</view>
					<view class="btn-cont-item btn-cont-itemB" @click="afterUpdate">以后再说</view>
				</view>
			</view>
		</view>
		<!-- 框 在这里   已经是最新版本 -->
		<view class="newed" v-if="showNewed">已经是最新版本</view>
	</view>
</template>

<script>
	var _this;
export default {	
	data() {
		_this = this;
		return {
			newVersion:'',
			note:[],
			url:'', 
			type:''
		};
	},
	props: {
		showTips: false, // 控制显示 提示框 默认为false 不显示
		showNewed: false // 控制显示 已经是最新版本 提示框 默
	},
	methods: {
		afterUpdate() {
			this.showTips = false;
		},
		nowUpdate(url, type) {
			console.log(url,type);
			this.showTips = false;
			if (type == 'apk') {
				plus.runtime.openURL(url);
			} else if (type == 'wgt') {
				plus.nativeUI.showWaiting('正在下载更新资源...');
				plus.downloader
					.createDownload(
						url,
						{
							filename: '_doc/update/'
						},
						function(d, status) {
							console.log(status);
							if (status == 200) {
								console.log('下载wgt成功：' + d.filename);
								installWgt(d.filename); // 安装wgt包
							} else {
								console.log('下载wgt失败！');
								plus.nativeUI.alert('下载资源失败！');
							}
							plus.nativeUI.closeWaiting();
						}
					)
					.start();

				function installWgt(path) {
					plus.nativeUI.showWaiting('安装wgt文件...');
					plus.runtime.install(
						path,
						{},
						function() {
							plus.nativeUI.closeWaiting();
							console.log('安装wgt文件成功！');
							plus.nativeUI.alert('应用资源更新完成！', function() {
								plus.runtime.restart();
							});
						},
						function(e) {
							plus.nativeUI.closeWaiting();
							console.log('安装wgt文件失败[' + e.code + ']：' + e.message);
							plus.nativeUI.alert('升级失败系统本版与资源包不匹配');
						}
					);
				}
			}
		}
	},
	onLoad() {
		plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
			uni.request({
				url: 'http://ftp.iccnet.cn/Public/qvnet/zzjy_2.0/version/parent.json',
				success: function(result) {
					var data = result.data.data;
					_this.newVersion = data.version;
					var currentVersions = widgetInfo.version.split('.');
					var resultVersions = data.version.split('.');

					var wgt_current_version = currentVersions[1] + '.' + currentVersions[2];
					var wgt_result_version = resultVersions[1] + '.' + resultVersions[2];

					if (currentVersions[0] < resultVersions[0]) {
						// 说明有大版本更新
						_this.note = data.note;
						this.showTips = true;
						//_this.afterUpdate(data.url, 'apk');
						_this.url = data.url;
						_this.type = 'apk';
						_this.$forceUpdate();
					} else if (parseFloat(wgt_current_version, 2) < parseFloat(wgt_result_version, 2)) {
						this.showTips = true;
						_this.note = data.wgtNote;
						_this.url = data.wgtUrl;
						_this.type = 'wgt';
						_this.$forceUpdate();
						//_this.afterUpdate(data.wgtUrl, 'wgt');
					}
				}
			});
		});
	}
};
</script>

<style></style>
